﻿#region Copyright (c) 2010 R. Uittenbosch
/*
* Copyright (C) 2010 R. Uittenbosch
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
* associated documentation files (the "Software"), to deal in the Software without restriction, including 
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
* following conditions:
* 
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Drawing;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsCE.Forms;
using LinkedService.Mobile;
using Microsoft.Practices.ServiceLocation;

namespace TouchUI.System.Mobile.Mvc
{
  public sealed class Common
  {
    public static readonly Common Instance = new Common();
    private IConfiguration Configuration 
    {
      get { return ServiceLocator.Current.GetInstance<IConfiguration>(); }
    }

    private float GetFontSize(string item, float def)
    {
      try
      {
        // First as string
        string val = Configuration.GetItem<string>(item,def.ToString());
        return float.Parse(val);
      }
      catch
      {
        try
        {
          // Or as integer
          int val = Configuration.GetItem<int>(item, (int)def);
          return float.Parse(val.ToString());
        }
        catch
        { }
      }
      return def;
    }

    private Common()
    {
      ZoomFactor            = Configuration.GetItem("ZoomFactor", 0);
      FontSizes             = new float[] { GetFontSize("NormalFontSize", 8.25F), GetFontSize("SmallFontSize", 6F) };
      FontFamilyName        = Configuration.GetItem<string>("FontFamily", "Trebuchet MS");
      UpdateZoom();
      Calibrate();
    }

    public void UpdateZoom()
    {

      if (NormalFont != null)     { NormalFont.Dispose(); }
      if (Font != null)           { Font.Dispose();}
      if (SubFont != null)        { SubFont.Dispose(); }
      if (SmallFont != null)      { SmallFont.Dispose(); }
      if (SmallBoldFont != null)  { SmallBoldFont.Dispose(); }

      // Increase/decrease fontsizes
      FontSizes[0] += (ZoomFactor * 2.0F);
      FontSizes[0] += (ZoomFactor * 2.0F);
      Configuration.SetItem("ZoomFactor", ZoomFactor);
      // Create fonts.
      NormalFont    = new Font(FontFamilyName, FontSizes[0], FontStyle.Regular);
      Font          = new Font(FontFamilyName, FontSizes[0], FontStyle.Bold);
      SubFont       = new Font(FontFamilyName, FontSizes[1], FontStyle.Bold);
      SmallFont     = new Font(FontFamilyName, FontSizes[1], FontStyle.Regular);
      SmallBoldFont = new Font(FontFamilyName, FontSizes[1], FontStyle.Bold);

    }

    public void Calibrate()
    {
      ScreenFactor          = 1;
      ScreenFactorSuffix    = string.Empty;
      ClientRectangle       = Screen.PrimaryScreen.WorkingArea;
      // Def
      ClientRectangle       = new Rectangle(Screen.PrimaryScreen.WorkingArea.X, Screen.PrimaryScreen.WorkingArea.Y, Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height - AdjustBottomHeigth);
      int maxScreenSide     = Math.Max(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
      if (maxScreenSide > 400)
      {
        ScreenFactor        = 2;
        ScreenFactorSuffix  = "2";
      }
      TouchDevice           = SystemSettings.Platform == WinCEPlatform.PocketPC;
      PaintBitmap           = new Bitmap(maxScreenSide, maxScreenSide);
      PaintCopyBitmap       = new Bitmap(maxScreenSide, maxScreenSide);
      PaintGraphics         = Graphics.FromImage(PaintBitmap);      
      PaintCopyGraphics     = Graphics.FromImage(PaintCopyBitmap);


      BlueBrush             = new SolidBrush(Color.FromArgb(0,102,153));
      FontBrush             = new SolidBrush(SystemColors.WindowText);
      GrayBrush             = new SolidBrush(Color.Gray);
      BackBrush             = new SolidBrush(SystemColors.Window);
      HeadBrush             = new SolidBrush(Color.FromArgb(0, 102, 153));
      HighBrush             = new SolidBrush(SystemColors.Highlight);
      HighTextBrush         = new SolidBrush(SystemColors.HighlightText);

      Pen                   = new Pen(SystemColors.WindowText, ScreenFactor);
      Pen2                  = new Pen(SystemColors.WindowText, 2 * ScreenFactor);
      HighTextPen2          = new Pen(SystemColors.HighlightText, 2 * ScreenFactor);
      MinimumItemHeight     = (int)(5 * FontSizes[1]); 
    }

    public int ZoomFactor { get; set; }
    public int MinimumItemHeight { get; set; }
    public string FontFamilyName { get; set; }
    public float[] FontSizes { get; set; }  
    public int AdjustBottomHeigth { get; set; }
    public int ScreenFactor { get; set; }
    public bool TouchDevice { get; set; }
    public string ScreenFactorSuffix { get; set; }
    public Bitmap PaintBitmap { get; set; }
    public Graphics PaintGraphics { get; set; }
    public Bitmap PaintCopyBitmap { get; set; }
    public Graphics PaintCopyGraphics { get; set; }
    public Font NormalFont { get; set; }
    public Font Font { get; set; }
    public Font SubFont { get; set; }
    public Font SmallFont { get; set; }
    public Font SmallBoldFont { get; set; }
    public Brush BlueBrush { get; set; }
    public Brush FontBrush { get; set; }
    public Brush GrayBrush { get; set; }
    public Brush HeadBrush { get; set; }
    public Brush BackBrush { get; set; }
    public Brush HighBrush { get; set; }
    public Brush HighTextBrush { get; set; }
    public Pen Pen { get; set; }
    public Pen Pen2 { get; set; }
    public Pen HighTextPen2 { get; set; }

    private Rectangle clientRectangle;
    public Rectangle ClientRectangle
    {
      get { return clientRectangle; }
      set
      {
        clientRectangle = value;
        clientRectangle.Y = 0;
      }
    }

   

    public string TruncateOneLine(Graphics g, string label, Font font, int totalWidth)
    {
      int width = totalWidth;
      SizeF size = g.MeasureString(label, font);
      if (size.Width < width)
        return label;
      width -= (int)(g.MeasureString("...", font).Width + 0.5);
      string s = label;
      while (size.Width > width)
      {
        s = s.Substring(0, s.Length - 1);
        size = g.MeasureString(s, font);
      }
      while (s.Length > 1 && s.EndsWith(" "))
        s = s.Substring(0, s.Length - 1);

      return s + "...";
    }
  }
}
